﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using AFS.VisitationRequestTool.Addin.Variables;
using AFS.VisitationRequestTool.Addin.Common;
using AFS.VisitationRequestTool.Addin.Entities;
using AFS.VisitationRequestTool.Addin.XML;
using AFS.VisitationRequestTool.Addin.Components;

namespace AFS.VisitationRequestTool.Addin.Dictionary
{
    public class DictionaryFactory
    {

        public static Dictionary<string, string> GetAppointmentDetailsDictionary(RequestDetailsFormRegion requestFormEntries, AppointmentItem appointmentItem)
        {
            //WindowFormRegionCollection formRegion = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveInspector()];            
            Dictionary<string, string> appointmentDetailsDictionary = new Dictionary<string, string>();
            string submitterID = String.Empty;
            string renewExpiredRequest = String.Empty;
            string agendaMaterials = String.Empty;

            //Outlook Fields/
            appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitTitle, appointmentItem.Subject);

            if (appointmentItem.IsRecurring)
            {
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitStartDate, Utility.GetInfoPathFormattedDateString(appointmentItem.GetRecurrencePattern().PatternStartDate));
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitEndDate, Utility.GetInfoPathFormattedDateString(appointmentItem.GetRecurrencePattern().PatternEndDate));
            }
            else
            {
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitStartDate, Utility.GetInfoPathFormattedDateString(appointmentItem.Start));
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitEndDate, Utility.GetInfoPathFormattedDateString(appointmentItem.End));
            }
            
            appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitDuration, Utility.GetCalculatedVisitDuration(appointmentItem.Duration));
            appointmentDetailsDictionary.Add(ElementNames.XmlNodeFrequencyOfVisit, Utility.GetFrequencyOfVisit(appointmentItem));
            appointmentDetailsDictionary.Add(ElementNames.XmlNodeRequestInitiation, General.TextVariableAFSPersonnel);
            renewExpiredRequest = requestFormEntries.chkExistingVisit.Checked ? "Yes" : "No";
            appointmentDetailsDictionary.Add(ElementNames.XmlNodeRenewExistingOrExpiredRequest, renewExpiredRequest);

            AddressEntry organizer = appointmentItem.GetOrganizer();
            string currentFileName = requestFormEntries.txtFileName_Hidden.Text.Trim();

            if (organizer != null)
            {
                MeetingOrganizer submitter = new MeetingOrganizer(appointmentItem.Session.CurrentUser);
                submitterID = submitter.AccountID; //email address.
            }

            appointmentDetailsDictionary.Add(ElementNames.XmlNodeSubmitterId, submitterID);

            // Social Visit Request    
            if (String.Equals(Utility.GetStringValueFromObject(requestFormEntries.lstVisitType.SelectedItem), General.VisitTypeSocialVisitRequest, StringComparison.OrdinalIgnoreCase))
            {
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitType, General.VisitTypeSocial);
                // string ddConfirmNoSensitiveShared.SelectedItem
                // Setting to default value "No" because got rid of ddConfirmNoSensitiveShared dropdown on form
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeSocialConfirmBusinessHasTakenPlace, General.SelectionNo);
                //appointmentDetailsDictionary.Add(XMLVariables.xmlNodeSocialVisitBusinessTakePlaceExplanation, userEntries.txtBusinessSharedExplanation.Text);
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeSocialVisitBusinessTakePlaceExplanation, requestFormEntries.txtSocialBusinessExplanation.Text);
                // Social_Discussion_Details
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeSocialDiscussionDetails, requestFormEntries.txtSocialBusinessExplanation.Text); // Changed from xmlNodeRecurringReasonForVisit   
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeRecurringReasonForVisit, requestFormEntries.txtSocialReasonForVisit.Text); // TODO: need to refactor this kludge.
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeForeignNational, Utility.GetStringValueFromObject(requestFormEntries.ddSocialForeignNationalYesOrNo.SelectedItem));
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeForeignNationalName, requestFormEntries.txtSocialForeignNationalEmail.Text.Trim());
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeAFSOfficeLocations, Utility.GetStringValueFromObject(requestFormEntries.ddSocialAFSOffices.SelectedItem));
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeValidateACNPersonnel, requestFormEntries.txtSocialNonAFSPersonnel.Text);
            }
            else // Non Social Visit Request 
            {
                if (appointmentItem.IsRecurring)
                {
                    appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitType, General.VisitTypeRecurring);                    
                }
                else
                    appointmentDetailsDictionary.Add(ElementNames.XmlNodeVisitType, General.VisitTypeOneTime); 

                appointmentDetailsDictionary.Add(ElementNames.XmlNodeForeignNational, Utility.GetStringValueFromObject(requestFormEntries.ddForeignNationalYesOrNo.SelectedItem));
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeForeignNationalName, requestFormEntries.txtForeignNationalEmail.Text.Trim());
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeAFSOfficeLocations, Utility.GetStringValueFromObject(requestFormEntries.ddAFSOffices.SelectedItem));
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeImpactIfRejected, requestFormEntries.txtImpact.Text);
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeValidateACNPersonnel, requestFormEntries.txtNonAFSPersonnel.Text);  // Hidden textbox
                // appointmentDetailsDictionary.Add(XMLVariables.xmlNodePreviousVisitRequestID, userEntries.txtPreviousVisitId.Text);
                // agendaMaterials = requestFormEntries.chkAgendaMaterials.Checked ? "Yes" : "No";
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeDoesMeetingIncludeAgenda, requestFormEntries.chkAgendaMaterials.Checked.ToString().ToLower());
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeRecurringReasonForVisit, requestFormEntries.txtReason.Text);
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeRecurringMoreThanEightPersonnel, "No");
                appointmentDetailsDictionary.Add(ElementNames.XmlNodeACNSharePointAccess,  Utility.GetStringValueFromObject(requestFormEntries.ddSharePointAccess.SelectedItem));

                //appointmentDetailsDictionary.Add(ElementNames.XmlNodeInPersonVisitLocations, Utility.GetListFromListbox(requestFormEntries.lstVisitLocations).ToString());
                //appointmentDetailsDictionary.Add(ElementNames.XmlNodeAFSOfficeLocations, Utility.GetListFromListbox(requestFormEntries.lstAFSOffices).ToString());             

            }

            return appointmentDetailsDictionary;
        }

        public static Dictionary<string, OlUserPropertyType> GetPropertyItemsDictionary()
        {
            Dictionary<string, OlUserPropertyType> propertyItemsDictionary = new Dictionary<string, OlUserPropertyType>();

            propertyItemsDictionary.Add(Property.RequestSubmitted, OlUserPropertyType.olYesNo);
            propertyItemsDictionary.Add(Property.VisitType, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.NonAFSPersonnel, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.ReasonForVisit, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.Impact, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.VisitLocations, OlUserPropertyType.olKeywords);
            propertyItemsDictionary.Add(Property.AFSVisitLocation, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.ForeignNationalYesNo, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.ForeignNationalNames, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.AgendaMaterials, OlUserPropertyType.olYesNo);
            propertyItemsDictionary.Add(Property.RenewExistingVisit, OlUserPropertyType.olYesNo);
            propertyItemsDictionary.Add(Property.PreviousVisitID, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.NonAFSPersonnelSocial, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.ReasonForVisitSocial, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.ConfirmSensitiveInfoSocial, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.ConfirmSensitiveInfoExplanationSocial, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.Filename, OlUserPropertyType.olText);
            propertyItemsDictionary.Add(Property.SharePointAccessRequired, OlUserPropertyType.olText);

            return propertyItemsDictionary;

        }

        public static Dictionary<string, string> GetAttachmentsBase64Dictionary(AppointmentItem appointmentItem)
        {
            Dictionary<string, string> attachmentsDictionary = new Dictionary<string, string>();

            foreach (Attachment attachment in appointmentItem.Attachments)
            {
                string fileName = attachment.FileName;
                string filePath = System.IO.Path.GetTempPath() + "\\" + fileName;
                Utility.SaveFileToTemporaryLocation(attachment, filePath);
                string base64File = Utility.GetBase64StringFromFile(filePath);

                attachmentsDictionary.Add(fileName, base64File);
                Utility.RemoveFileFromTemporaryLocation(filePath);
            }

            return attachmentsDictionary;
        }

        public static Dictionary<string, List<MeetingRecipient>> GetRecipientsDictionary(AppointmentItem appointmentItem)
        {
            Dictionary<string, List<MeetingRecipient>> recipientsDictionary = new Dictionary<string, List<MeetingRecipient>>();
            List<MeetingRecipient> afsMeetingRecipients = new List<MeetingRecipient>();
            List<MeetingRecipient> acnMeetingRecipients = new List<MeetingRecipient>();
            //Recipient organizer = appointmentItem.Session.CurrentUser;
            WindowFormRegionCollection formRegion = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveInspector()];
            RequestDetailsFormRegion userEntries = formRegion.AFSRequestDetailsForm;

            if (appointmentItem.Recipients.Count > 0)
            {
                foreach (Recipient recipient in appointmentItem.Recipients)
                {
                    MeetingRecipient meetingRecipient;

                    if (!String.Equals(appointmentItem.Session.CurrentUser.Name, recipient.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        meetingRecipient = new MeetingRecipient(recipient);
                    }
                    else
                    {
                        meetingRecipient = new MeetingOrganizer(recipient);
                    }

                    if (meetingRecipient.AccountType == UserType.AFS)
                        afsMeetingRecipients.Add(meetingRecipient);
                    else
                        acnMeetingRecipients.Add(meetingRecipient);
                }
            }

            if (String.Equals(userEntries.lstVisitType.SelectedItem.ToString(),
                   "Social Visit Request", StringComparison.OrdinalIgnoreCase))
            {
                recipientsDictionary.Add(ElementNames.XmlNodeSocialAFSPersonnelInvolved, afsMeetingRecipients);
                recipientsDictionary.Add(ElementNames.XmlNodeSocialValidateACNPersonnel, acnMeetingRecipients);
            }
            else
            {
                // Recurring/one-time meeting form currently selected
                recipientsDictionary.Add(ElementNames.XmlNodeAFSPersonnelInvolved, afsMeetingRecipients);
                recipientsDictionary.Add(ElementNames.XmlNodeValidateACNPersonnel, acnMeetingRecipients);
                //recipientsDictionary.Add(ElementNames.XmlNodeValidateAFSPersonnel, afsMeetingRecipients);                
            }

            return recipientsDictionary;
        }

    }
}
